<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="nav-header">
			<view class="nav-bar">
				<view class="nav-left" @click="cancel">
					<uni-icons type="left" size="24" color="#FFFFFF"></uni-icons>
				</view>
				<view class="nav-title">信息修改</view>
				<view class="nav-right">
					<uni-icons type="ellipsis" size="24" color="#FFFFFF"></uni-icons>
				</view>
			</view>

			<!-- 占位区域 -->
			<view class="search-placeholder"></view>
		</view>

		<!-- 内容区域 -->
		<view class="content-view">
			<scroll-view class="content-scroll" scroll-y>
				<!-- 头像选择区域 -->
			<view class="avatar-selection">
				<!-- 操作按钮栏 -->
				<view class="action-bar">
					<text class="cancel-btn" @click="cancel">取消</text>
					<text class="confirm-btn" @click="confirm">确认</text>
				</view>
				
				<!-- 头像网格和上传按钮整合 -->
				<view class="avatar-grid">
					<!-- 预定义头像选项 -->
					<view class="avatar-option" v-for="(avatar, index) in avatarOptions" :key="index" @click="selectAvatar(index)">
						<view class="avatar-radio" :class="{ active: selectedAvatar === index }">
							<image class="avatar-img" :src="avatar.url" mode="aspectFill"></image>
							<view class="radio-dot" v-if="selectedAvatar === index"></view>
						</view>
					</view>
					
					<!-- 上传照片按钮 - 整合到网格中 -->
					<view class="avatar-option">
						<view class="upload-btn" @click="uploadPhoto">
							<uni-icons type="plus" size="40" color="#CCCCCC"></uni-icons>
							<text class="upload-text">上传照片</text>
						</view>
					</view>
				</view>
			</view>
			</scroll-view>
		</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue';
	
	const selectedAvatar = ref(0);
	
	// 预定义头像选项
	const avatarOptions = ref([
		{ url: '../../../static/image/我的-我的@2x.png' },
		{ url: '../../../static/image/首页@2x.png' },
		{ url: '../../../static/image/消息中心@2x.png' },
		{ url: '../../../static/image/保养@2x.png' }
	]);
	
	// 取消操作
	const cancel = () => {
		uni.navigateBack();
	};
	
	// 确认操作
	const confirm = () => {
		uni.navigateTo({
			url: '/pages/my/personalInformation/editInformation/submitInformation/submitInformation'
		});
	};
	
	// 选择头像
	const selectAvatar = (index) => {
		selectedAvatar.value = index;
	};
	
	// 上传照片
	const uploadPhoto = () => {
		uni.chooseImage({
			count: 1,
			success: (res) => {
				// 这里可以处理上传照片的逻辑
				console.log('选择的照片路径:', res.tempFilePaths[0]);
				// 假设上传成功后，将新照片添加到选项中并选中
				avatarOptions.value.push({ url: res.tempFilePaths[0] });
				selectedAvatar.value = avatarOptions.value.length - 1;
			},
			fail: (err) => {
				console.log('选择照片失败:', err);
			}
		});
	};
</script>

<style scoped>
	.container {
		width: 100%;
		height: 100vh;
		display: flex;
		flex-direction: column;
		background: #f5f5f5;
		overflow: hidden;
	}

	/* 顶部导航栏 */
	.nav-header {
		background: linear-gradient(180deg, #2477F4 0%, #166BEC 100%);
		padding-top: 30rpx;
		padding-bottom: 30rpx;
	}

	.nav-bar {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 30rpx;
		padding-top: calc(20rpx + env(safe-area-inset-top));
		position: relative;
	}

	.nav-left {
		width: 80rpx;
		display: flex;
		align-items: center;
		cursor: pointer;
	}

	.nav-title {
		flex: 1;
		text-align: center;
		font-size: 36rpx;
		font-weight: 500;
		color: #fff;
		position: absolute;
		left: 0;
		right: 0;
		pointer-events: none;
	}

	.nav-right {
		width: 80rpx;
		display: flex;
		align-items: center;
		justify-content: flex-end;
	}

	.ml-20 {
		margin-left: 20rpx;
	}

	/* 占位区域 */
	.search-placeholder {
		height: 250rpx;
	}

	/* 内容视图 */
	.content-view {
		flex: 1;
		padding: 0 30rpx 20rpx 30rpx;
		margin-top: -250rpx;
		height: 0;
		overflow: visible;
		display: flex;
		flex-direction: column;
	}

	.content-scroll {
		flex: 1;
		height: 0;
		width: 100%;
	}

	/* 头像选择区域 */
	.avatar-selection {
		background: #fff;
		border-radius: 20rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.06);
		overflow: hidden;
		margin: 0 20rpx;
	}
	
	/* 头像网格 */
	.avatar-grid {
		padding: 30rpx;
	}
	
	/* 操作按钮栏 */
	.action-bar {
		display: flex;
		justify-content: space-between;
		padding: 30rpx;
		position: relative;
	}
	
	/* 分割线样式 - 不顶满卡片，有左右边距 */
	.action-bar::after {
		content: '';
		position: absolute;
		left: 30rpx;
		right: 30rpx;
		bottom: 0;
		height: 3rpx;
		background-color: #E5E5E5;
	}
	
	.cancel-btn {
		color: #333333;
		font-size: 28rpx;
	}
	
	.confirm-btn {
		color: #1E88E5;
		font-size: 28rpx;
	}
	
	/* 头像网格 */
	.avatar-grid {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		gap: 20rpx;
		margin-bottom: 30rpx;
	}
	
	/* 头像选项 */
	.avatar-option {
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	/* 头像单选框 */
	.avatar-radio {
		position: relative;
		width: 160rpx;
		height: 160rpx;
		border-radius: 12rpx;
		overflow: hidden;
		border: 2rpx solid #EEEEEE;
	}
	
	.avatar-radio.active {
		border-color: #1E88E5;
	}
	
	/* 头像图片 */
	.avatar-img {
		width: 100%;
		height: 100%;
	}
	
	/* 单选圆点 */
	.radio-dot {
		position: absolute;
		top: 8rpx;
		right: 8rpx;
		width: 24rpx;
		height: 24rpx;
		border-radius: 50%;
		background-color: #1E88E5;
		border: 4rpx solid #FFFFFF;
	}
	
	/* 上传区域 */
	.upload-section {
		display: flex;
		justify-content: center;
	}
	
	/* 上传按钮 */
	.upload-btn {
		width: 160rpx;
		height: 160rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		border: 2rpx dashed #CCCCCC;
		border-radius: 12rpx;
	}
	
	.upload-text {
		font-size: 24rpx;
		color: #999999;
		margin-top: 10rpx;
	}
</style>
